<template>
  <div class="userManage">
    <div class="header">
      <h1>用户管理</h1>
      <div class="header-right">
        <i class="el-icon-s-home"></i>
        <span class="font-left">首页</span>
        <i class="el-icon-arrow-right"></i>
        <span class="font-right">用户管理</span>
      </div>
    </div>
    <div class="content">
      <div class="bar">
        <div class="barLeft">
          <el-button
            type="success"
            icon="el-icon-plus"
            size="mini"
            @click="addUser"
            >新增</el-button
          >
          <el-button
            type="info"
            icon="el-icon-lollipop"
            size="mini"
            @click="dialogFormVisible = true"
            >高级搜索</el-button
          >
          <download-excel style="display: inline-block; margin: 0 10px;" :data="tableData" :fields="json_fields" :header="headers" :name="name">
          <el-button type="success" icon="el-icon-upload" size="mini"
            >导出</el-button
          >
          </download-excel>
          <el-button type="info" size="mini" @click="resetList">
            <i class="el-icon-refresh-left"></i>
            重置</el-button
          >
        </div>
        <div class="barRight">
          <el-input
            @keyup.enter.native="search"
            clearable
            @clear="search"
            v-model="searchContent"
            placeholder="查找名称"
            class="barInput"
            size="mini"
          ></el-input>
          <el-button
            slot="append"
            icon="el-icon-search"
            size="mini"
            @click="search"
          ></el-button>
        </div>
      </div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column
          prop="id"
          label="ID"
          width="60"
          sortable
        ></el-table-column>
        <!-- <el-table-column prop="img" label="" width="60"></el-table-column> -->
        <el-table-column
          prop="departmentName"
          label="部门"
          width="210"
        ></el-table-column>
        <el-table-column
          prop="realName"
          label="真实姓名"
          width="210"
        ></el-table-column>
        <el-table-column
          prop="username"
          label="用户名"
          width="210"
        ></el-table-column>
        <el-table-column
          prop="roleName"
          label="角色"
          width="250"
        ></el-table-column>
        <el-table-column
          prop="phoneNum"
          label="电话"
          width="300"
        ></el-table-column>
        <el-table-column
          prop="salary"
          label="工资"
          width="230"
          sortable
        ></el-table-column>
        <el-table-column prop="operate" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="changeUser(scope.row)"
              >修改</el-button
            >
            <el-button size="mini" type="danger" @click="goDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        @getPagination="getPagination"
        :total="total"
        :pageSize="pageSize"
      ></Pagination>

      <!-- --高级搜索dialog -->
      <el-dialog :visible.sync="dialogFormVisible" width="40%">
        <el-form :model="dialogForm">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item class="select" prop="department">
                <span slot="label">
                  <span class="labelText">部门</span>
                </span>
                <el-select
                  v-model="dialogForm.departmentId"
                  placeholder="--选择类型--"
                  style="width: 350.59px"
                >
                  <el-option
                    v-for="item in depArr"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item prop="realName">
                <span slot="label">
                  <span class="labelText">真实姓名</span>
                </span>
                <el-input v-model="dialogForm.realName"></el-input>
              </el-form-item>
              <el-form-item prop="phoneNum">
                <span slot="label">
                  <span class="labelText">联系电话</span>
                </span>
                <el-input v-model="dialogForm.phoneNum"></el-input>
              </el-form-item>
              <el-form-item>
                <span slot="label">
                  <span class="labelText">生日起</span>
                </span>
                <el-input v-model="dialogForm.startBirthday"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item class="select">
                <span slot="label">
                  <span class="labelText">角色</span>
                </span>
                <el-select
                  v-model="dialogForm.roleId"
                  placeholder="--选择状态--"
                  style="width: 350.59px"
                >
                  <el-option
                    v-for="item in roleArr"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <span slot="label">
                  <span class="labelText">用户名</span>
                </span>
                <el-input v-model="dialogForm.username"></el-input>
              </el-form-item>
              <el-form-item>
                <span slot="label">
                  <span class="labelText">身份证号</span>
                </span>
                <el-input v-model="dialogForm.identityNum"></el-input>
              </el-form-item>
              <el-form-item>
                <span slot="label">
                  <span class="labelText">生日止</span>
                </span>
                <el-input v-model="dialogForm.endBirthday"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="highSearch"
            >搜索</el-button
          >
          <el-button @click="reset">重置</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import Pagination from "@/components/Pagination";
export default {
  name: "",
  components: {
    Pagination,
  },
  mixins: [],
  props: {},
  data() {
    return {
      input: "",
      total: 0,
      currentPage: 1,
      pageSize: 10,
      //搜索内容
      searchContent: "",
      //暂存表格数据
      stashList: "",
      //高级搜索弹窗默认消失
      dialogFormVisible: false,
      //高级搜索搜索内容
      dialogForm:{
        departmentId:'',
        roleId:'',
        realName:'',
        username:'',
        phoneNum:'',
        identityNum:'',
        startBirthday:'',
        endBirthday:''
      },
      //表格抬头的名字
      headers: "用户管理表格",
      //表格名称
      name: "用户管理表",
      //表头对应的数据属性
      json_fields: {
        部门: "department",
        真实姓名: "name",
        用户名: "username",
        角色: "character",
        手机号码: "phone",
        工资: "money",
      },
      json_data:[],
      tableData: [],
      depArr: [],
      roleArr: [],
      value: "",
      form: {},
    };
  },
  computed: {},
  watch: {},
  mounted() {},
  created() {
    this.getAllUserList(this.currentPage, this.pageSize);
    this.getDepArr()
    this.getOccupationArr()
  },
  methods: {
    resetList() {
      this.getAllUserList(this.currentPage, this.pageSize);
    },
    //获取点击的页码
    getPagination(page) {
      this.currentPage = page;
      this.getAllUserList(this.currentPage, this.pageSize);
    },
    //获取表格数据
    async getAllUserList(currentPage, pageSize) {
      let res = await this.$api.user.getUserList({ currentPage, pageSize });
      console.log(res);
      if (res.data.code === 200) {
        this.tableData = res.data.data.list;
        // console.log(this.tableData);
        //页码
        this.total = res.data.data.total;
      } else {
        console.log("请求失败");
        this.tableData = [];
        this.pageSize = 0;
        this.total = 1;
      }
    },
    //搜索
    async search() {
      let res = await this.$api.user.searchUserByName(this.currentPage,this.pageSize,
        this.searchContent.toString()
      );
      this.tableData = res.data.data.list;
      this.total = res.data.data.total;
    },
    //高级搜索
    async highSearch(){
      this.dialogForm.currentPage=this.currentPage;
      this.dialogForm.pageSize=this.pageSize;
      console.log(this.dialogForm);
      let res =await this.$api.user.highLevelSearch(this.dialogForm)
      this.tableData=res.data.data.list;
      this.dialogFormVisible=false;
      console.log(res.data.data);
    },
    //高级搜索重新设置
    reset() {
      this.dialogForm={
        currentPage: 1,
        pageSize: 10,
        departmentId:'',
        roleId:'',
        realName:'',
        username:'',
        phoneNum:'',
        identityNum:'',
        startBirthday:'',
        endBirthday:''
      }
    },
    //去新增页面
    addUser() {
      this.$router.push("/user/addUser");
    },
    //更改页面
    changeUser(row) {
      this.$router.push({
        name: "changeUser",
        params: {
          row,
        },
      });
    },
    //删除
    goDelete(id) {
      console.log(id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteConfirm(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async deleteConfirm(id) {
      let res = await this.$api.user.deleteUser(id);
      console.log(res);
      if (res.data.code === 200) {
        this.$message({
          type: "success",
          message: "删除成功!",
        });
        //更新视图-------------------------
        this.getAllUserList(this.currentPage, this.pageSize);
      } else {
        this.$message.error("删除失败");
      }
    },
    //获取部门选项
    async getDepArr(){
      let res = await this.$api.user.getDeptList1()
      let arrData = res.data.data
      arrData.forEach(element => {
        this.depArr.push({label:element.depName,value:element.depId})
      })
    },
    //获取角色选项
    async getOccupationArr(){
      let res = await this.$api.user.getOccupationList1()
      let arrData = res.data.data.list
      console.log(arrData);
      arrData.forEach(element => {
        this.roleArr.push({label:element.roleName,value:element.id})
      })
    },
    
  },
};
</script>
<style lang="less" scoped>
.header {
  position: relative;
  padding: 15px;
  display: flex;
  justify-content: space-between;
}
.header > h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 700;
}
.header-right {
  font-size: 12px;
  display: flex;
  align-items: center;
}
.el-icon-arrow-right {
  padding: 0 5px;
  color: #ccc;
}
.el-icon-s-home {
  color: #444;
  display: inline-block;
  margin-right: 5px;
}
.font-left {
  color: #444;
}
.font-right {
  color: #777;
}

.content {
  background-color: #f5f5f5;
}
.bar {
  display: flex;
  justify-content: space-between;
  background-color: #fff;
  padding: 10px;
  border-top: 3px solid #d2d6de;
  border-bottom: 1px solid #f4f4f4;
}
.barLeft{
  display: flex;
}
.barRight {
  display: flex;
}
.el-button-success {
  background-color: red;
}
.el-table-column {
  color: #333333;
  height: 37px;
}
.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fafafa;
  line-height: 30px;
}
.footerLeft {
  display: flex;
  margin-left: 5px;
  font-size: 12px;
}
.footerLeft > div {
  padding: 2px;
}
.mid {
  display: inline-block;
  margin: 0 5px;
}
.footerLeft > div > span {
  padding: 0 10px;
}
.footerRight {
  display: flex;
}
.footerBtn {
  width: 30px;
  height: 25px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
